package makeconfig

import (
	"fmt"
	"io/ioutil"
	"wolongauth/errhelp"

	"github.com/dingdinglz/dingtools/dingjson"
	"github.com/fzdwx/infinite"
	"github.com/fzdwx/infinite/components/input/text"
	"github.com/fzdwx/infinite/components/selection/confirm"
	"github.com/fzdwx/infinite/components/selection/singleselect"
	"github.com/gofiber/fiber/v2"
)

func Run() {
	jsonmap := fiber.Map{}
	web := infinite.NewText(text.WithPrompt("WebPort(服务器端口):"))
	web.Display()
	jsonmap["WebPort"] = web.Value()
	debugi := infinite.NewConfirmWithSelection(confirm.WithDefaultYes(), confirm.WithPrompt("Debug模式:"))
	debug_value, _ := debugi.Display()
	jsonmap["Debug"] = debug_value
	httpsi := infinite.NewConfirmWithSelection(confirm.WithPrompt("启用Https:"))
	https_value, _ := httpsi.Display()
	jsonmap["Https"] = https_value
	if https_value {
		keyi := infinite.NewText(text.WithPrompt("https key文件:"))
		keyi.Display()
		jsonmap["Https_Key"] = keyi.Value()
		certi := infinite.NewText(text.WithPrompt("https cert文件:"))
		certi.Display()
		jsonmap["Https_Cert"] = certi.Value()
		sendtohttpsi := infinite.NewConfirmWithSelection(confirm.WithDefaultYes(), confirm.WithPrompt("强制https:"))
		sendtohttpsi_value, _ := sendtohttpsi.Display()
		jsonmap["SendToHttps"] = sendtohttpsi_value
		if sendtohttpsi_value {
			sendporti := infinite.NewText(text.WithPrompt("强制http-https端口:"), text.WithDefaultValue("80"))
			sendporti.Display()
			jsonmap["SendPort"] = sendporti.Value()
		}
	}
	adminusers := []fiber.Map{}
	fmt.Println("创建admin用户，在username处输入stop停止创建")
	usernamei := infinite.NewText(text.WithPrompt("UserName:"))
	passwordi := infinite.NewText(text.WithPrompt("Password:"))
	for {
		usernamei.Display()
		if usernamei.Value() == "stop" {
			break
		}
		passwordi.Display()
		adminusers = append(adminusers, fiber.Map{"username": usernamei.Value(), "password": passwordi.Value()})
		usernamei.Reset()
		passwordi.Reset()
	}
	jsonmap["AdminUser"] = adminusers
	var database_array_flag = -1
	database_array := []string{"mysql", "sqlite"}
	databasei := infinite.NewSingleSelect(database_array, singleselect.WithPrompt("选用数据库类型:"))
	for database_array_flag == -1 {
		database_array_flag, _ = databasei.Display()
	}
	database_value := database_array[database_array_flag]
	jsonmap["Database"] = database_value
	{
		if database_value == "mysql" {
			mysql_databasei := infinite.NewText(text.WithPrompt("mysql库名:"))
			mysql_databasei.Display()
			jsonmap["Mysql_Database"] = mysql_databasei.Value()
			mysql_useri := infinite.NewText(text.WithPrompt("mysql账号:"))
			mysql_useri.Display()
			jsonmap["Mysql_User"] = mysql_useri.Value()
			mysql_passi := infinite.NewText(text.WithPrompt("mysql密码:"))
			mysql_passi.Display()
			jsonmap["Mysql_Pass"] = mysql_passi.Value()
		}
		if database_value == "sqlite" {
			sqlite_databasei := infinite.NewText(text.WithPrompt("sqlite文件名:"))
			sqlite_databasei.Display()
			jsonmap["Sqlite_Database"] = sqlite_databasei.Value()
		}
	}
	titlei := infinite.NewText(text.WithPrompt("网站title:"))
	titlei.Display()
	jsonmap["Title"] = titlei.Value()
	ensuretoknowi := infinite.NewText(text.WithPrompt("注册用户须知文件(txt):"))
	ensuretoknowi.Display()
	jsonmap["EnsureToKnow"] = ensuretoknowi.Value()

	j, err := dingjson.NewFromStruct(&jsonmap)
	errhelp.ErrDoWithTagAndAbort(err, "config json maker")
	fmt.Println(j.ConventToStr())
	err = ioutil.WriteFile("config.json", j.GetSrc(), 0777)
	errhelp.ErrDoWithTagAndAbort(err, "write config json")
}
